#include "lrcform.h"
#include "qdebug.h"
#include "qevent.h"
#include "ui_lrcform.h"
#include <QTextCodec>

lrcForm::lrcForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::lrcForm)
{
    ui->setupUi(this);

    lrcAnimetionhide = new QPropertyAnimation(this,"geometry");
    lrcAnimetionhide->setStartValue(QRect(12, 12, width(), height()));
    lrcAnimetionhide->setEndValue(QRect(12, height() + 12, width(), height()));
    lrcAnimetionhide->setDuration(250);

    connect(lrcAnimetionhide,&QPropertyAnimation::finished,this,[=](){hide();});

    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        lrcAnimetionhide->start();
    });

    QFont lyricFont("Microsoft YaHei");
    for (int i = 1; i <= 10; i++) {
        QLabel *label = findChild<QLabel*>(QString("label_%1").arg(i));
        if (label) {
            label->setFont(lyricFont);
            label->setStyleSheet("color: white;background:transparent;font:20px;");
        }
    }
    ui->label_7->setStyleSheet("font:30px;color: white;background:transparent;");
    clear();
}

lrcForm::~lrcForm()
{
    delete ui;
}

void lrcForm::setTitle(QString title)
{
    ui->label_1->setText(title);
}

void lrcForm::setAuthor(QString author)
{
    ui->label_2->setText(author);
}

void lrcForm::setAlbum(QString album)
{
    ui->label_3->setText(album);
}

void lrcForm::clear()
{
    ui->label_4->setText("");
    ui->label_5->setText("");
    ui->label_6->setText("");
    ui->label_7->setText("");
    ui->label_8->setText("");
    ui->label_9->setText("");
    ui->label_10->setText("");
}


bool lrcForm::parseLrc(QString path)
{
    QFile lyricFile(path);
    lyricFile.open(QFile::ReadOnly);

    lines.clear();

    while(!lyricFile.atEnd())
    {
        //按行读取
        //[xx:xx.xx]
        QString line = lyricFile.readLine(1024);
        int start = 0,end = 0;
        end = line.indexOf(']',start);
        QString time = line.mid(start,end-start+1);
        QString word = line.mid(end+1);
        qint64 lineTime = 0;
        //分
        start = 1;
        end = time.indexOf(':', start);
        lineTime += time.mid(start, end - start).toInt()*60*1000;
        //秒
        start = end+1;
        end = time.indexOf('.', start);
        lineTime += time.mid(start, end - start).toInt()*1000;
        //毫秒
        start = end+1;
        end = time.indexOf('.', start);
        lineTime += time.mid(start, end - start).toInt();

        lines.push_back(Lrcline(lineTime,word));
    }
    std::stable_sort(lines.begin(), lines.end());
    lyricFile.close();
    return true;
}

bool operator <(const Lrcline& a, const Lrcline& b)
{
    return a.time < b.time;
}

int lrcForm::getIndex(qint64 pos)
{
    if(lines.size() == 0)
        return -1;
    else
    {
        if(lines[0].time >= pos)
        {
            return 0;
        }
    }
    for(int i = 1; i != lines.size(); i++)
    {
        if(lines[i - 1].time < pos && lines[i].time >= pos)
        {
            return i - 1;
        }
    }
    return lines.size() - 1;
}

void lrcForm::showLrc(qint64 pos)
{
    int index = getIndex(pos);
    // 获取失败，则显示当前歌曲无歌词
    if(index == -1)
    {
        ui->label_4->setText("");
        ui->label_5->setText("");
        ui->label_6->setText("");

        ui->label_7->setText(u8"当前歌曲无歌词");

        ui->label_8->setText("");
        ui->label_9->setText("");
        ui->label_10->setText("");
    }
    else
    {
        ui->label_4->setText(getLrcText(index-3));
        ui->label_5->setText(getLrcText(index-2));
        ui->label_6->setText(getLrcText(index-1));

        ui->label_7->setText(getLrcText(index));

        ui->label_8->setText(getLrcText(index+1));
        ui->label_9->setText(getLrcText(index+2));
        ui->label_10->setText(getLrcText(index+3));
    }
    QCoreApplication::processEvents();
}

QString lrcForm::getLrcText(int index)
{
    // 下标不在范围内，则返回空字符串
    if(index < 0 || index >= lines.size())
        return "";
    else
        return lines[index].text;
}


